package org.jeecg.modules.simulator.rooter;

import lombok.Getter;
import lombok.Setter;
import org.jeecg.modules.simulator.Simulator;
import org.jeecg.modules.simulator.jsonot.PsgStationDistributionInfo;
import org.jeecg.modules.simulator.jsonot.PsgStationDistributionStType;
import org.jeecg.modules.simulator.jsonot.RouteStation;
import org.jeecg.modules.simulator.model.SceneDataType;
import org.jeecg.modules.simulator.model.SceneParam;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.person.Person;
import org.jeecg.modules.simulator.model.person.path.PersonPath;
import org.jeecg.modules.simulator.model.person.path.PersonPathItem;
import org.jeecg.modules.simulator.model.person.path.PersonState;
import org.jeecg.modules.simulator.util.UnitCache;


import java.time.LocalTime;
import java.util.*;

/**
 * @program: simulator-b20240918
 * @description: 人员创建
 * @author: renzhg
 * @create: 2024-12-09 09:35
 **/
public class PersonCreater implements Simulator {
    @Setter
    @Getter
    String sceneCode;
    Map<String, List<RouteStation>> t80RouteHisMap = new HashMap<>();
    //key:路径序号+首站点名称
    Map<String, PersonPath> personPathMap = new HashMap<>();
    //key:路径序号+首站点id
    Map<String, PersonPath> personPathIdMap = new HashMap<>();
    @Getter
    @Setter
    Map<String, List<PsgStationDistributionInfo>> psgStationDistributionInfoMap = new HashMap<>();

    @Getter
    @Setter
    //key为routeid
    Map<String,PersonPath> route2PersonPathMap = new HashMap<>();
    @Getter
    @Setter
    //总station集合，使用的是stationId,做key
    Map<String, Station> stationMap = new HashMap<>();
    //换乘站名称字典集合
    @Getter
    @Setter
    Map<String, TransferStation> transferNameStationMap = new HashMap<>();

    @Getter
    @Setter
    //换乘站集合
    Map<String, TransferStation> transferStationMap = new HashMap<>();


    public void assignRoute2PersonPath(Map<String, List<RouteStation>> t80RouteHisMap){
        t80RouteHisMap.forEach((k,v)->{
            v.sort((a, b) -> a.getStationSn().compareTo(b.getStationSn()));
            List<PersonPathItem> personPathItemList = generatePersonPathItemList(v, k);
            PersonPath personPath = new PersonPath(personPathItemList);
            route2PersonPathMap.put(k,personPath);
        });

    }
    /**
     * @description: 根据提供的路线站点和路线ID生成PersonPathItem对象的列表
     *
     * @author:  renzhg
     * @date:  2024/12/9 9:44
     **/
    private List<PersonPathItem> generatePersonPathItemList(List<RouteStation> routeStations, String routeId) {
        List<PersonPathItem> personPathItems = new ArrayList<>();
        for (int n = 0; n < routeStations.size(); n++) {
            PersonPathItem personPathItem = new PersonPathItem();
            String stationId = routeStations.get(n).getStationId();
            personPathItem.setStationId(stationId);

            String name = stationMap.get(stationId).getName();
            personPathItem.setStationName(name);
            personPathItem.setIndex(n);
            transferStationMap.forEach((k, v) -> {
                if (v.isInTransferStation(stationId)) {
                    personPathItem.setTransfer(true);
                } else {
                    personPathItem.setNeedTransfer(false);
                }

            });
            if (routeStations.get(n).getTripDrctCd().isEmpty()) {
                personPathItem.setNeedTransfer(true);
            } else {
                personPathItem.setNeedTransfer(false);
            }
            personPathItem.setPathIndex(routeId);

            personPathItems.add(personPathItem);


        }
        return personPathItems;
    }

    /**
     * @description: 设定了每个站的走行速度
     *生成人员，并且放置到站内走行集合和候车集合
     * @author:  renzhg
     * @date:  2024/12/9 10:47
     **/
    private void stationInitAddPersons(){
        stationMap.forEach((k, v) -> {
            v.setSceneCode(sceneCode);
            v.init();

            List<PsgStationDistributionInfo> psgStationDistributionInfoList = psgStationDistributionInfoMap.get(v.getId());
            /**
             * @description: 这个位置缺乏的就是对进站的人员没有分类，比如，分为正在进站的，还是已经进站开始等车的人。
             * 这个需要对进站的人通过对比进站的时间和当前时间，来判断是进站还是候车，进站过程中已经有多少时长了，需要讲站点信息输入到人员信息中
             * 这块功能以后补充
             *
             * @author: renzhg
             * @date: 2024/10/8 17:21
             **/
            psgStationDistributionInfoList.forEach(p -> {
                String routeId = p.getRouteId();
                //                有问题route2PersonPathMap包含的站点名称
                PersonPath personPath=route2PersonPathMap.get(routeId);
                Person person = new Person(personPath);
                person.setId(UUID.randomUUID().toString());
                if (p.getStationType().equals(PsgStationDistributionStType.start)) {
                    person.setPersonState(PersonState.inStation);
                    v.getOutsidePersonMap().put(person.getId(), person);

                } else if (p.getStationType().equals(PsgStationDistributionStType.end)) {
                    person.setPersonState(PersonState.offStation);
                    person.setOutOutputWalkTime(v.getOutWalk2StationSide());

                    v.getOutStationPersonMap().put(person.getId(), person);
                }

            });
            System.out.println("完成");
        });
    }
/**
 * @description: 生成换乘人员并把生成的人员加入到换乘站的换乘人员集合中
 *
 * @author:  renzhg
 * @date:  2024/12/9 10:53
 **/
    private void transferAddPersons(Map<String, List<RouteStation>> t80RouteHisMap) {
        Map<String,PersonPath> route2PersonPathMap1 = new HashMap<>();
        route2PersonPathMap.forEach((k,v)->{
            String key=k.split("_")[0];
            route2PersonPathMap1.put(key,v);
        });

        transferStationMap.forEach((k, v) -> {
            v.getTransferStationMap().forEach((kk, vv) -> {
                Map<String, List<Person>> personListMap = new HashMap<>();
                List<PsgStationDistributionInfo> psgStationDistributionInfoList = psgStationDistributionInfoMap.get(vv.getId());
                psgStationDistributionInfoList.forEach(p -> {
                    String routeId = p.getRouteId();

                    PersonPath personPath =route2PersonPathMap1.get(routeId);
                    if(personPath!=null){
                        String nodeStr = personPath.getNodeStr(vv.getName());
                        Person person = new Person(personPath);
                        person.setId(UUID.randomUUID().toString());
                        if (p.getStationType().equals(PsgStationDistributionStType.transfer)) {
                            person.setPersonState(PersonState.transfer);
                            if (personListMap.containsKey(nodeStr)) {
                                personListMap.get(nodeStr).add(person);
                            } else {
                                List<Person> personList1 = new ArrayList<>();
                                personList1.add(person);
                                personListMap.put(nodeStr, personList1);

                            }
                        }
                    }

                });
                v.getTransferPersonsMap().putAll(personListMap);

            });

        });
    }


    @Override
    public LocalTime getCurrentTime() {
        return null;
    }

    @Override
    public void UpdateCurrentTime(LocalTime currentTime) {

    }

    @Override
    public void nextTime(String now, Integer step) {
        SceneParam sceneParam = UnitCache.sceneParamMap.get(sceneCode);
        stationMap.forEach((k, v) -> {
            if (sceneParam.getSceneType().equals(SceneDataType.customJson)) {
            } else if (sceneParam.getSceneType().equals(SceneDataType.allDayHistoryData)) {
            }
        });
    }



}
